package rdbms;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import DAO.DatabasePool;
import DAO.ENSequenceDAO;


import model.ENSequence;
import model.Relation;

public class RDBMSRelationDAO {
	
	private String idConnection;
	private ENSequenceDAO sequence;
	private final String name = "RELATION";

	private static String INSERT = "insert into RELATION (RELATION_ID, DESCRIPTION ) values (?, ?)";

	private static String DELETE = "delete from RELATION where RELATION_ID = ? ";

	private static String DELETE_ALL = "delete from RELATION";

	private static String SELECT_BY_ID = "select DESCRIPTION "
			+ "from RELATION where RELATION_ID = ? ";

	private static String SELECT_ALL = "select * from RELATION";

	private static String UPDATE = "update RELATION set DESCRIPTION = ? where RELATION_ID = ?";

	public RDBMSRelationDAO(final String idConnection, final ENSequenceDAO sequence) throws SQLException {
		this.idConnection = idConnection;
		this.sequence = sequence;
	}
	
	public boolean insert(Relation relation) throws Exception {
		boolean success = false;
		Connection conn = null;
		PreparedStatement pst = null;
		try {
			conn = DatabasePool.getConnection(idConnection);
			relation.setRelationId(sequence.nextval(ENSequence.RELATION_ID));
			pst = conn.prepareStatement(INSERT);
			pst.setInt(1, relation.getRelationId());
			pst.setString(2, relation.getDescription());
			pst.execute();
			success = true;
		} catch (Exception e) {
			throw new Exception("Exception while inserting "+name, e);
		} finally {
			if (success) {
				conn.commit();
				System.out.println(name+" Insert - Commit");
			} else {
				conn.rollback();
				System.out.println(name+" Insert - Rollback");
			}
			if (pst != null)
				pst.close();
		}
		return success;
	}

	public boolean delete(int id) throws Exception {
		boolean success = false;
		Connection conn = null;
		PreparedStatement pst = null;
		try {
			conn = DatabasePool.getConnection(idConnection);
			pst = conn.prepareStatement(DELETE);
			pst.setInt(1, id);
			pst.execute();
			success = true;
		} catch (Exception e) {
			throw new Exception("Exception while deleting "+name, e);
		} finally {
			if (success) {
				conn.commit();
				System.out.println(name+" - Delete - Commit");
			} else {
				conn.rollback();
				System.out.println(name+" - Delete - Rollback");
			}
			if (pst != null)
				pst.close();
		}
		return success;
	}

	public boolean deleteAll() throws Exception {
		boolean success = false;
		Connection conn = null;
		PreparedStatement pst = null;
		try {
			conn = DatabasePool.getConnection(idConnection);
			pst = conn.prepareStatement(DELETE_ALL);
			pst.execute();
			success = true;
		} catch (Exception e) {
			throw new Exception("Exception while deleting(all) "+name, e);
		} finally {
			if (success) {
				conn.commit();
				System.out.println(name+" - Delete(all) - Commit");
			} else {
				conn.rollback();
				System.out.println(name+" - Delete(all) - Rollback");
			}
			if (pst != null)
				pst.close();
		}
		return success;
	}

	public Relation find(int id) throws Exception {
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rss = null;
		Relation relation = null;
		try {
			conn = DatabasePool.getConnection(idConnection);
			pst = conn.prepareStatement(SELECT_BY_ID);
			pst.setInt(1, id);
			rss = pst.executeQuery();
			if (rss.next()) {
				relation = new Relation(id, rss.getString(1));
			}
			return relation;
		} catch (Exception e) {
			throw new Exception("Exception while retrieving "+name, e);
		} finally {
			if (rss != null)
				rss.close();
			if (pst != null)
				pst.close();
		}
	}

	public boolean update(Relation relation) throws Exception {
		boolean success = false;
		Connection conn = null;
		PreparedStatement pst = null;
		try {
			conn = DatabasePool.getConnection(idConnection);
			pst = conn.prepareStatement(UPDATE);
			pst.setString(1, relation.getDescription());
			pst.setInt(2, relation.getRelationId());
			pst.execute();
			success = true;
		} catch (Exception e) {
			throw new Exception("Exception while updating "+name, e);
		} finally {
			if (success) {
				conn.commit();
				System.out.println(name+" - Update - Commit");
			} else {
				conn.rollback();
				System.out.println(name+" - Update - Rollback");
			}
			if (pst != null)
				pst.close();
		}
		return success;
	}
	
	
	public List<Relation> list(int noi) throws Exception {
		List<Relation> list = null;
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rss = null;
		int ctr = 0;
		try {
			conn = DatabasePool.getConnection(idConnection);
			list = new ArrayList<Relation>();
			pst = conn.prepareStatement(SELECT_ALL);
			rss = pst.executeQuery();
			while (rss.next()) {
				if (++ctr > noi)
					break;
				Relation relation = new Relation(rss.getInt(1), rss.getString(2));
				list.add(relation);
			}
			return list;
		} catch (Exception e) {
			throw new Exception("Exception while retrieving "+name, e);
		} finally {
			if (rss != null)
				rss.close();
			if (pst != null)
				pst.close();
		}
	}
}
